package com.lc.YYGH.hosp.controller.api;


import com.alibaba.excel.util.StringUtils;

import com.lc.YYGH.common.exception.YyghException;
import com.lc.YYGH.common.result.Result;
import com.lc.YYGH.common.result.ResultStatus;
import com.lc.YYGH.common.utils.MD5;

import com.lc.YYGH.helper.HttpRequestHelper;
import com.lc.YYGH.hosp.service.DepartmentService;
import com.lc.YYGH.hosp.service.HospitalService;

import com.lc.YYGH.hosp.service.HospitalSetService;
import com.lc.YYGH.hosp.service.ScheduleService;
import com.lc.YYGH.model.hosp.Department;
import com.lc.YYGH.model.hosp.Hospital;
import com.lc.YYGH.model.hosp.Schedule;
import com.lc.YYGH.vo.hosp.DepartmentQueryVo;
import com.lc.YYGH.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.print.attribute.ResolutionSyntax;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags="医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;


    @Autowired
    private ScheduleService scheduleService;
//把上传的医院信息上传到mongo数据库中
    @PostMapping("saveHospital")
    public Result save(HttpServletRequest request)

    {
        Map<String,String[]> parameterMap=request.getParameterMap();
        Map<String,Object> map= HttpRequestHelper.switchMap(parameterMap);
//        //签名校验
//
//        //医院系统传递过来的签名key，做了MD5加密的
//        String hospSignKey=String.valueOf(map.get("sign"));
//
//
//
//        //根据Hoscode查询的签名key 未加密
//        String hoscode=(String)map.get("hoscode");
//        String signKey1=hospitalSetService.getSignKey(hoscode);
//        //加密
//        String signKeyMD5=MD5.encrypt(signKey1);
//
//        //校验
//        if(!hospSignKey.equals(signKeyMD5))
//        {
//            System.out.println("签名不匹配");
//            throw new YyghException(ResultStatus.SIGN_ERROR);
//
//        }
//        //如果一致顺利的调用save方法保存
//
//
//        //图片处理
        String logoDataStr=(String) map.get("logoData");
        //" "转换成“+”
//        if(!StringUtils.isEmpty(logoDataStr))
//        {
//            String logoData=logoDataStr.replaceAll(" ","+");
//            map.put("logoData",logoData);
//        }
        map.put("logoData",logoDataStr);
        System.out.println("test2-------------");
        hospitalService.save(map);
        return Result.success();
    }
    @ApiOperation(value="获取医院信息")
    @PostMapping("hospital/show")
    public Result findHospital(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String,Object> map=HttpRequestHelper.switchMap(parameterMap);
        String hoscode=(String)map.get("hoscode");
        //异常处理
//        if(StringUtils.isEmpty(hoscode))
//        {
//            throw new YyghException(ResultStatus.FAIL);
//        }
        //签名校验
//        String hospSignkey=String.valueOf(map.get("sign"));
//        String hoscode1=(String)map.get("hoscode");
//
//        String signKey = hospitalSetService.getSignKey(hoscode);
//        String signKeyMD5=MD5.encrypt(signKey);
//        if(!hospSignkey.equals(signKey))
//        {
//            throw new YyghException(ResultStatus.SIGN_ERROR);
//        }
        //调用service中根据医院编码获取医院的方法
        Hospital hospital=hospitalService.findHospital(hoscode);
        return Result.success(hospital);
    }

    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request)
    {

        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String,Object> map=HttpRequestHelper.switchMap(parameterMap);
        //签名校验
        //调用service中的方法
        departmentService.save(map);
        return Result.success();
    }
    @PostMapping("department/list")
    public Result findDepartmentsByPage(HttpServletRequest request)
    {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String,Object> map=HttpRequestHelper.switchMap(parameterMap);
        String hoscode=(String)map.get("hoscode");
        //签名校验
        //页号
        Integer page = StringUtils.isEmpty(map.get("page")) ? 1 :
                Integer.parseInt((String) map.get("page"));
        //每页数据条数
        Integer limit = StringUtils.isEmpty(map.get("limit")) ? 10 :
                Integer.parseInt((String) map.get("limit"));
        DepartmentQueryVo dVo=new DepartmentQueryVo();
        dVo.setHoscode(hoscode);
      Page<Department> pageModel1= departmentService.findPageDepartment(page,limit,dVo);
      return Result.success(pageModel1);

    }
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request)
    {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String,Object> map=HttpRequestHelper.switchMap(parameterMap);
        String hoscode=(String)map.get("hoscode");
        String depcode=(String)map.get("depcode");
        //签名校验(同签名)、
        departmentService.removeDepartment(hoscode,depcode);
        return Result.success();

    }

    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request)
    {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String,Object> map=HttpRequestHelper.switchMap(parameterMap);
        String hoscode=(String)map.get("hoscode");
        //验证签名key
        scheduleService.save(map);
        return Result.success();
    }
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request)
    {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String,Object> map=HttpRequestHelper.switchMap(parameterMap);
        Integer page = StringUtils.isEmpty(map.get("page")) ? 1 :
                Integer.parseInt((String) map.get("page"));
        //每页数据条数
        Integer limit = StringUtils.isEmpty(map.get("limit")) ? 10 :
                Integer.parseInt((String) map.get("limit"));
        String hoscode =(String)map.get("hoscode");
        ScheduleQueryVo scheduleQueryVo=new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        Page<Schedule> pageModel1= scheduleService.selectPage(page,limit,scheduleQueryVo);
        return Result.success(pageModel1);
    }

    @PostMapping("schedule/remove")
    public Result removeShedule(HttpServletRequest request)
    {
        Map<String,String[]> parameterMap=request.getParameterMap();
        Map<String,Object> map=HttpRequestHelper.switchMap(parameterMap);
        String hoscode=(String) map.get("hoscode");
        String hosScheduleId=(String) map.get("hosScheduleId");
        scheduleService.removeSchedule(hoscode,hosScheduleId);
        return Result.success();
    }


}
